import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)
const Home = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
const Login = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
const Welcome = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

const Users = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue')
const Rights = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import( /* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

const GoodsCate = () => import( /* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
const GoodsParams = () => import( /* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')

const GoodsList = () => import( /* webpackChunkName: "GoodsList_Add" */ '../components/goods/List.vue')
const GoodsAdd = () => import( /* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

const Order = () => import( /* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
const Report = () => import( /* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')
const routes = [{
    path: "/",
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import('../components/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/Welcome',
    component: Home,
    children: [{
        path: '/welcome',
        name: 'welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'users',
        component: Users
      },
      {
        path: '/rights',
        name: 'rights',
        component: Rights
      },
      {
        path: '/roles',
        name: 'roles',
        component: Roles
      },
      {
        path: '/categories',
        name: 'GoodsCate',
        component: GoodsCate
      },
      {
        path: '/params',
        name: 'GoodsParams',
        component: GoodsParams
      },
      {
        path: '/goods',
        name: 'GoodsList',
        component: GoodsList,
      },
      {
        path: '/goods/add',
        name: 'GoodsAdd',
        component: GoodsAdd,
      },
      {
        path: '/orders',
        name: 'order',
        component: Order,
      },
      {
        path: '/Reports',
        name: 'Report',
        component: Report,
      },
    ]
  },
  {
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  //如果访问登录页，直接return放行
  if (to.path === '/login') return next()
  // 获取token, 不存在则强制跳转登录页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})


export default router